package com.wg.msorderds.Maaper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wg.msorderds.entity.OrderProductInfo;
import com.wg.msorderds.entity.Ordertb;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

@Mapper
public interface OrdertbMapper extends BaseMapper<Ordertb> {
    @Select("SELECT * FROM ordertb WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<Ordertb> selectByUserId(Integer userId);
    @Select("SELECT * FROM ordertb WHERE status = 0 AND create_time < #{timeoutTime}")
    List<Ordertb> selectTimeoutUnpaidOrders(Date timeoutTime);

    @Update("UPDATE ordertb SET status = 1 WHERE id = #{orderId}")
    int updateStatusToPaid(Integer orderId);

    @Select("SELECT " +
            "    o.id AS orderId, " +  // 订单ID，映射到实体类的orderId属性
            "    p.main_img AS manIng, " +  // 商品主图，映射到实体类的manIng属性
            "    s.shop_name AS shopName,"+  // 店铺名称，映射到实体类的shopName属性
            "o.status,"+  // 订单状态，映射到实体类的status属性
            "od.id as odId,"+  // 订单详情ID，映射到实体类的odId属性
            "    p.`name`, " +  // 商品名称，映射到实体类的name属性
            "    od.count, " +  // 商品数量，映射到实体类的count属性
            "    od.sku, " +  // 商品规格，映射到实体类的sku属性
            "    od.count * p.price_after AS actualPayment, " +  // 实付款总金额（数量×优惠后单价）
            "    od.count * p.price_before AS originalPayment, " +  // 优惠前总金额（数量×优惠前单价）
            "    o.create_time AS createTime , " +  // 订单创建时间，映射到实体类的createTime属性
            " p.id "+
            "FROM " +
            "    ordertb o " +  // 订单表，别名o
            "JOIN " +
            "    orderdetails od ON o.id = od.order_id " +  // 关联订单详情表，条件：订单ID相等
            "JOIN " +
            "    product p ON od.product_id = p.id " +  // 关联商品表，条件：商品ID相等
            "JOIN " +
            "    shop s ON od.shop_id = s.id " +  // 关联店铺表，条件：店铺ID相等
            "WHERE " +
            "    o.user_id = #{userId} "+          // 查询条件：根据用户ID筛选
            "ORDER BY o.id;"
    )
    List<OrderProductInfo> getOrderProductsByUserId(Integer userId);
}
